using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Lyxite.CatKingdom
{
    class KeyListener
    {
        public event EventHandler EnabledChanged;

        static Dictionary<Keys, Actions> _keyactions = new Dictionary<Keys, Actions>();

        static KeyListener()
        {
            _keyactions.Add(Keys.Decimal, Actions.Toggle);

            _keyactions.Add(Keys.NumPad0, Actions.Clear);

            _keyactions.Add(Keys.NumPad1, Actions.DivideDownLeft);
            _keyactions.Add(Keys.NumPad3, Actions.DivideDownRight);
            _keyactions.Add(Keys.NumPad7, Actions.DivideUpLeft);
            _keyactions.Add(Keys.NumPad9, Actions.DivideUpRight);

            _keyactions.Add(Keys.NumPad2, Actions.MoveDown);
            _keyactions.Add(Keys.NumPad4, Actions.MoveLeft);
            _keyactions.Add(Keys.NumPad6, Actions.MoveRight);
            _keyactions.Add(Keys.NumPad8, Actions.MoveUp);

            _keyactions.Add(Keys.NumPad5, Actions.LeftClick);
            _keyactions.Add(Keys.Divide, Actions.MiddleClick);
            _keyactions.Add(Keys.Multiply, Actions.RightClick);

            _keyactions.Add(Keys.Add, Actions.Enlarge);
            _keyactions.Add(Keys.Subtract, Actions.Shrink);
        }

        MouseSimulator cat = new MouseSimulator();

        bool enabled = true;

        private void Toggle()
        {
            Enabled = !enabled;

            //if (enabled)
            //{
            //    UpdateCursor();
            //}

            //Draw();
        }

        public void KeyDown(object sender, KeyEventArgs e)
        {
            Keys key = e.KeyCode;
            if (!enabled && !(_keyactions.ContainsKey(key) && _keyactions[key] == Actions.Toggle))
                return;

            bool handled = false;

            if (_keyactions.ContainsKey(key))
            {
                Actions action = _keyactions[key];
                if (action == Actions.Toggle)
                    Toggle();
                else
                    cat.Process(action, false);
                handled = true;
            }

            e.Handled = handled;
        }

        public void KeyUp(object sender, KeyEventArgs e)
        {
            Keys key = e.KeyCode;
            if (!enabled && !(_keyactions.ContainsKey(key) && _keyactions[key] == Actions.Toggle))
                return;

            if (_keyactions.ContainsKey(key))
            {
                Actions action = _keyactions[key];
                if (action != Actions.Toggle)
                    cat.Process(action, true);
                e.Handled = true;
            }
        }

        public void KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
                if (EnabledChanged != null)
                    EnabledChanged(this, new EventArgs());
            }
        }
    }
}
